package tech.windanchaos.blog.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tech.windanchaos.blog.entity.*;
import tech.windanchaos.blog.mapper.ArticleMapper;
import tech.windanchaos.blog.service.ArticleService;

import java.util.ArrayList;
import java.util.List;

@Component
public class ArticleServiceImpl implements ArticleService {
    @Autowired(required = true)
    ArticleMapper articleMapper;

    @Override
    public List<ArticleDes> getArticlesByPage(int start, int limit) {
        return articleMapper.getArticlesByPage(start, limit);
    }

    @Override
    public List<ArticleDes> getArticlesByPageAdmin(Integer start, Integer limit) {
        return articleMapper.getArticlesByPageAdmin(start, limit);
    }

    @Override
    public List<ArticleDes> getAllArticles() {
        return articleMapper.getAllArticles();
    }

    @Override
    public List<ArticleCategory> getArticleCategoryList() {
        return articleMapper.getArticleCategoryList();
    }

    @Override
    public List<ArticleArchive> getArticleArchive(int start, int limit) {
        return articleMapper.getArticleArchive(start, limit);
    }

    @Override
    public List<Category> getCategoriesByArticleID(int id) {
        return articleMapper.getCategoriesByArticleID(id);
    }

    @Override
    public Category getCategoryByID(int id) {
        return articleMapper.getCategoryByID(id);
    }

    @Override
    public Article getArticleByID(int id) {
        return articleMapper.getArticleByID(id);
    }

    @Override
    public List<ArticleDes> getArticleDesByCategoryID(int id) {
        return articleMapper.getArticleDesByCategoryID(id);
    }

    @Override
    public List<ArticleTag> getArticleTagList() {
        return articleMapper.getArticleTagList();
    }

    @Override
    public List<ArticleDes> getArticleDesByTagID(Integer id) {
        return articleMapper.getArticleDesByTagID(id);
    }

    @Override
    public Tag getTagByID(Integer id) {
        return articleMapper.getTagByID(id);
    }

    @Override
    public List<Category> getAllCategory() {
        return articleMapper.getAllCategory();
    }

    @Override
    public List<ArticleDes> searchBlogByCategoryIdAndTitle(SearchBody searchBody) {
        return articleMapper.searchBlogByCategoryIdAndTitle(searchBody);
    }

    @Override
    public Category getCategoryByName(String name) {
        return articleMapper.getCategoryByName(name);
    }

    @Override
    public void addCategory(String name) {
        articleMapper.addCategory(name);
    }

    @Override
    public void updateCategory(Category category) {
        articleMapper.updateCategory(category);
    }

    @Override
    public void delCategory(Integer id) {
        articleMapper.delCategory(id);
    }

    @Override
    public List<Tag> getAllTags() {
        return articleMapper.getAllTags();
    }

    @Override
    public Tag getTagByName(String name) {
        return articleMapper.getTagByName(name);
    }

    @Override
    public void addTag(String name) {
        articleMapper.addTag(name);
    }

    @Override
    public void updateTag(Tag tag) {
        articleMapper.updateTag(tag);
    }

    @Override
    public void delTag(Integer id) {
        articleMapper.delTag(id);
    }

    @Override
    public Article getArticleById(Integer id) {
        return articleMapper.getArticleById(id);
    }

    @Override
    public List<Category> getArticleCategoryById(Integer id) {
        return articleMapper.getArticleCategoryById(id);
    }

    @Override
    public List<Tag> getArticleTagById(Integer id) {
        return articleMapper.getArticleTagById(id);
    }

    @Override
    public Integer insertArticle(Article blog) {
        articleMapper.insertArticle(blog);
        return blog.getId().intValue();
    }

    @Override
    public void updateArticle(Article article) {
        articleMapper.updateArticle(article);
    }

    @Override
    public List<Integer> splitValues(String values) {
        List<Integer> list = new ArrayList<>();
        if (values.contains(",")) {
            String[] tmp = values.split(",");
            for (int i = 0; i < tmp.length; i++) {
                list.add(Integer.valueOf(tmp[i]));
            }
        } else {
            list.add(Integer.valueOf(values));
        }
        return list;

    }

    @Override
    public String commitCategoryValues(List<Category> values) {
        StringBuffer buffer = new StringBuffer();
        values.forEach(category -> buffer.append(category.getId() + ","));
        return buffer.toString().substring(0, buffer.length());
    }

    @Override
    public String commitTagsValues(List<Tag> values) {
        StringBuffer buffer = new StringBuffer();
        values.forEach(tag -> buffer.append(tag.getId() + ","));
        return buffer.toString().substring(0, buffer.length());
    }

    @Transactional
    @Override
    public void categoryUpdate(Integer id, List<Integer> categoryIds) {
        articleMapper.deleteArticleCategory(id);
        articleMapper.insertArticleCategory(id, categoryIds);
    }

    @Transactional
    @Override
    public void tagsUpdate(Integer id, List<Integer> tagIds) {
        articleMapper.deleteArticleTags(id);
        articleMapper.insertArticleTags(id, tagIds);
    }

    @Override
    public void deleteArticle(Integer id) {
        articleMapper.deleteArticle(id);
        articleMapper.deleteArticleTags(id);
        articleMapper.deleteArticleTags(id);
    }
}
